package com.cq.hd.app.biz;

import cn.hutool.core.bean.BeanUtil;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.ResultGenerator;
import com.cq.hd.member.api.TbBusinessApi;
import com.cq.hd.member.api.TbBusinessSubscribeApi;
import com.cq.hd.member.api.vo.BusinessVo;
import com.cq.hd.product.api.TbActiveApi;
import com.cq.hd.product.api.vo.SearchBusinessActiveVo;
import com.cq.hd.product.api.vo.UserSubscribeBusinessVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author much
 * @since 2024/10/18 23:02
 */
@Component
public class BusinessSubscribeBiz {

    @Resource
    private TbBusinessSubscribeApi businessSubscribeApi;
    @Resource
    private TbBusinessApi businessApi;
    @Resource
    private TbActiveApi activeApi;

    // 获取用户订阅商家列表
    public Result<List<UserSubscribeBusinessVo>> getUserSubscribeBusinessList(Long userId) {
        // 查询用户订阅的商家列表
        List<Long> businessIds = businessSubscribeApi.listByUserId(userId).unpack();
        if (CollectionUtils.isEmpty(businessIds)) {
            return ResultGenerator.success(Collections.emptyList());
        }
        List<BusinessVo> businessVos = businessApi.listByIds(businessIds).unpack();

        // 统计商家订阅人数
        Map<Long, Integer> businessIdToSubscribeNumMap = businessSubscribeApi.getBusinessSubscribeCount(businessIds).unpack();
        // 查询商家进行中的活动列表
        Map<Long, Integer> businessInProgressActiveMap = getBusinessInProgressActiveMap(businessIds);

        List<UserSubscribeBusinessVo> resultList = businessVos.stream()
                .map(businessVo -> {
                    UserSubscribeBusinessVo userSubscribeBusinessVo = new UserSubscribeBusinessVo();
                    userSubscribeBusinessVo.setBusinessId(businessVo.getId());
                    userSubscribeBusinessVo.setBusinessLogo(businessVo.getLogo());
                    userSubscribeBusinessVo.setBusinessShortName(businessVo.getBusinessShortName());
                    userSubscribeBusinessVo.setSubscribeUserNum(businessIdToSubscribeNumMap.getOrDefault(businessVo.getId(), 0));
                    userSubscribeBusinessVo.setInProgressActiveNum(businessInProgressActiveMap.getOrDefault(businessVo.getId(), 0));
                    return userSubscribeBusinessVo;
                })
                .collect(Collectors.toList());

        return ResultGenerator.success(resultList);
    }

    private Map<Long, Integer> getBusinessInProgressActiveMap(List<Long> businessIds) {
        List<SearchBusinessActiveVo> searchBusinessActives = activeApi.listBusinessInProgressActives(businessIds).unpack();
        Map<Long, Integer> resultMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(searchBusinessActives)) {
            for (SearchBusinessActiveVo searchBusinessActiveVo : searchBusinessActives) {
                Long businessId = searchBusinessActiveVo.getBusinessId();
                Integer num = resultMap.getOrDefault(businessId, 0);
                resultMap.put(businessId, num + 1);
            }
        }
        return resultMap;
    }

}
